package com.dsdl.chatapp.module.chat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dsdl.chatapp.R;
import com.dsdl.chatapp.contacts.Constant;
import com.dsdl.chatapp.data.SocketMessage;
import com.dsdl.chatapp.common.utils.SPUtil;
import com.dsdl.chatapp.common.utils.StringUtils;

import java.util.List;

/**
 * Created by zzg on 2016/9/5.
 * 使用ListView的聊天页面的适配器
 */
public class ChatAdapter extends BaseAdapter {
    Context mContext;
    private List<SocketMessage> mList;
    private LayoutInflater mLayoutInflater;

    public ChatAdapter(Context context, List<SocketMessage> list) {
        mContext = context;
        mList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList == null ? null : mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mLayoutInflater.inflate(R.layout.item_chat_window, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        initData(holder, position);
        return view;
    }

    private void initData(ViewHolder holder, int position) {
        if (mList == null) {
            return;
        }
        if (mList.size() <= position) {
            return;
        }
        if (SPUtil.getSharedStringData(Constant.USER_PHONE, "").equals(mList.get(position).getFromUser())) {
            holder.llSend.setVisibility(View.VISIBLE);
            holder.llReceived.setVisibility(View.GONE);
            holder.tvSendMsg.setText(mList.get(position).getContent());
        } else {
            holder.llSend.setVisibility(View.GONE);
            holder.llReceived.setVisibility(View.VISIBLE);
            holder.tvReceivedMsg.setText(mList.get(position).getContent());
        }
        long currentPositionTimestamp = mList.get(position).getCreateTime();
        String time = StringUtils.timestamp2DateTime(currentPositionTimestamp);
        if (position > 0){

        }else {
            holder.tvTime.setText(time);
            holder.tvTime.setVisibility(View.VISIBLE);
        }

    }

    class ViewHolder {
        LinearLayout llReceived;
        ImageView ivReceiverPhoto;
        TextView tvReceivedMsg;
        LinearLayout llSend;
        TextView tvSendMsg;
        ImageView ivSenderPhoto;
        TextView tvTime;

        public ViewHolder(View itemView) {
            llReceived = (LinearLayout) itemView.findViewById(R.id.ll_item_received);
            ivReceiverPhoto = (ImageView) itemView.findViewById(R.id.iv_item_receiver_photo);
            tvReceivedMsg = (TextView) itemView.findViewById(R.id.tv_item_received_msg);
            llSend = (LinearLayout) itemView.findViewById(R.id.ll_item_send);
            tvSendMsg = (TextView) itemView.findViewById(R.id.tv_item_send_msg);
            ivSenderPhoto = (ImageView) itemView.findViewById(R.id.iv_item_sender_photo);
            tvTime = (TextView) itemView.findViewById(R.id.tv_item_time);
        }
    }
}
